
package com.borqs.music.ui;

import java.util.List;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.borqs.music.MediaPlaybackService;
import com.borqs.music.R;
import com.borqs.music.util.MusicUtils;
import com.borqs.music.util.MusicUtils.ServiceToken;

/**
 * Common base for fixed-content ListView
 */
public abstract class FixedEntryListActivity extends ListActivity implements
        MusicUtils.Defs, ServiceConnection {

    protected ServiceToken mToken;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entry_list);
        mToken = MusicUtils.bindToService(this.getParent(), this);
        MusicUtils.addNowPlayingListener(this);

        CategoryAdapter adapter = new CategoryAdapter(this, createListItem());
        setListAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter f = new IntentFilter();
        f.addAction(MediaPlaybackService.META_CHANGED);
        f.addAction(MediaPlaybackService.QUEUE_CHANGED);
        registerReceiver(mTrackListListener, f);
        mTrackListListener.onReceive(null, null);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mTrackListListener);
    }

    @Override
    protected void onDestroy() {
        if (mToken != null) {
            MusicUtils.unbindFromService(mToken);
        }
        super.onDestroy();
    }

    private BroadcastReceiver mTrackListListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            MusicUtils.updateNowPlaying(FixedEntryListActivity.this);
        }
    };

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MusicUtils.updateNowPlaying(this);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        finish();
    }

    public static class CategoryAdapter extends ArrayAdapter<ListViewItem> {
        private static class ViewHolder {
            TextView textView;
            ImageView iconView;
        }

        private static final int layoutId = R.layout.home_list;
        private LayoutInflater inflater;

        public CategoryAdapter(Context context, List<ListViewItem> items) {
            super(context, 0, items);
            inflater = LayoutInflater.from(context);
        }

        @Override
        public long getItemId(int position) {
            return getItem(position).id;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View returnView;
            ViewHolder holder;

            if (convertView == null) {
                returnView = inflater.inflate(layoutId, parent, false);
                holder = new ViewHolder();
                holder.textView = (TextView) returnView.findViewById(R.id.text1);
                holder.iconView = (ImageView) returnView.findViewById(R.id.listicon1);
                returnView.setTag(holder);
            } else {
                returnView = convertView;
                holder = (ViewHolder) returnView.getTag();
            }
            ListViewItem item = getItem(position);
            holder.textView.setText(item.textResId);
            holder.iconView.setImageResource(item.iconResId);
            return returnView;
        }
    }

    public static class ListViewItem {
        public int id;
        public int textResId;
        public int iconResId;
    }

    /**
     * Please override this method to give your list item data.
     * 
     * @return
     */
    protected abstract List<ListViewItem> createListItem();
}
